﻿/*
 * @file: OccasionDA.cs
 * @in namespace: DAL
 */
using System;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for OccasionDA
/// </summary>
namespace DAL
{
    public class OccasionDA : BaseDAL, IOccasion
    {
        private const string tableName = "Occasions";
        private const string occasionID = "OccasionID";
        private const string occasionName = "OccasionName";
        private const string isEnable = "IsEnable";

        public OccasionDA()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static int OccasionNameExisted(string _occasionName) {
            try
            {
                return RecordExisted(tableName, occasionName, _occasionName);
            }
            catch (Exception ex)
            { 
                throw ex;
            }
        }

        #region IOccasion Members

        /*
         * @method: GetOccasions
         * @description: get all Occasions
         * @input: allowFilter <TRUE enables ignore hidden records>
         * @output: all records corresponding
         */
        public Occasion[] GetOccasions(bool allowFilter)
        {
            Occasion[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (allowFilter)
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = 1",
                        tableName, isEnable);
                else
                    cmd.CommandText = String.Format("SELECT * FROM {0}", tableName);

                String[] columnNames = {    occasionID,
                                            occasionName,
                                            isEnable};
                result = SelectCollection<Occasion>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: GetOccasionByOccasionID
         * @description: get Occasion by OccasionID
         * @input: OccasionID, allowFilter <TRUE enables ignore hidden records>
         * @output: record corresponding
         */
        public Occasion GetOccasionByOccasionID(int _occasionID, bool allowFilter) {
            Occasion[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (allowFilter)
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = 1",
                        tableName, occasionID, occasionID, isEnable);
                    cmd.Parameters.Add(String.Format("@{0}", occasionID), SqlDbType.Int).Value = _occasionID;
                }
                else
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                        tableName, occasionID, occasionID);
                    cmd.Parameters.Add(String.Format("@{0}", occasionID), SqlDbType.Int).Value = _occasionID;
                }

                String[] columnNames = {    occasionID,
                                            occasionName,
                                            isEnable};
                result = SelectCollection<Occasion>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (result.Length < 0)
                return result[0];
            else
                return null;
        }

        /*
         * @method: InsertOccasion
         * @description: insert new Occasion to database
         * @input: Occasion entity
         * @output: 1 if insert successful, 0 if error occurred
         */
        public int InsertOccasion(Occasion _occasion, out int autoNumber)
        {
            int result = 0;
            try
            {                
                
                String[] columnNames = {  occasionName  };

                Object[] values = { _occasion.OccasionName };
                
                result = BaseDAL.InsertTableWithReturnID(tableName, columnNames, values, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: DisableOccasion
         * @description: hidden a occasion
         * @input: occasionID, isEnable <FALSE if hidden>
         * @output: 1 if insert successful, 0 if error occurred
         */
        public int DisableOccasion(int _occasionID, bool _isEnable)
        {
            int result = 0;
            try
            {
                String[] columnNames = { isEnable };
                Object[] values = { _isEnable };
                String[] keyColumnNames = { occasionID };
                Object[] keyColumnValues = { _occasionID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: UpdateOccasion
         * @description: update Occasion information
         * @input: Occasion entity
         * @output: 1 if successful, 0 if error occurred
         */
        public int UpdateOccasion(Occasion _occasion)
        {
            int result = 0;
            try
            {
                
                String[] columnNames = { occasionName };
                Object[] values = { _occasion.OccasionName };
                String[] keyColumnNames = { occasionID };
                Object[] keyColumnValues = { _occasion.OccasionID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion
    }
}